package com.simple.group;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.itemlib.BaseGroupAdapter;
import com.simple.R;

import java.util.Random;

/**
 * Copyright (C), 2016-2020,呼我出行网络科技有限公司
 * FileName: ItemAdapter
 * Author: Administrator
 * Date: 2020/10/30 10:40
 * Description:
 * History:
 */
public class GroupAdapter extends BaseGroupAdapter<Team, GroupAdapter.TeamHolder, GroupAdapter.ChildHolder> {
    public GroupAdapter(Context context) {
        super(context);
    }

    @Override
    public TeamHolder onCreateTeamHolder(ViewGroup parent, int viewType) {
        return new TeamHolder(inflate(R.layout.team_layout));
    }

    @Override
    public ChildHolder onCreateChildHolder(ViewGroup parent, int viewType) {
        return new ChildHolder(inflate(R.layout.child_layout));
    }

    @Override
    public void onBindViewTeamHolder(@NonNull TeamHolder holder, int teamPosition) {
        Team team = getItem(teamPosition);
        holder.nameTv.setText(team.teamName);
    }

    @Override
    public void onBindViewChildHolder(@NonNull ChildHolder holder, int teamPosition, int position) {
        Team team = getItem(teamPosition);
        if (team == null || team.teams.size() == 0) {
            holder.childTv.setText("无" + new Random().nextInt());
        } else {
            holder.childTv.setText(team.teams.get(position).name);
        }

    }


    @Override
    public int getChildCount(Team team) {
        return team.teams == null ? 0 : team.teams.size();
    }

    class TeamHolder extends RecyclerView.ViewHolder {
        TextView nameTv;

        public TeamHolder(@NonNull View itemView) {
            super(itemView);
            nameTv = itemView.findViewById(R.id.team_name_tv);
        }
    }

    class ChildHolder extends RecyclerView.ViewHolder {

        TextView childTv;

        public ChildHolder(@NonNull View itemView) {
            super(itemView);
            childTv = itemView.findViewById(R.id.child_name_tv);
        }
    }
}
